<template>
  <div class="container">
    <h2>水果购物车</h2>
    <div class="form-group">
      <label>顾客类型：</label>
      <select v-model="option" class="select">
        <option :value="1">A（苹果、草莓，无优惠）</option>
        <option :value="2">B（苹果、草莓、芒果，无优惠）</option>
        <option :value="3">C（苹果、草莓、芒果，草莓8折）</option>
        <option :value="4">D（苹果、草莓、芒果，草莓8折+满100减10）</option>
      </select>
    </div>
    <div
        v-for="fruit in availableFruits"
        :key="fruit.name"
        class="form-group fruit-row"
    >
      <label>{{ fruit.name }}（{{ fruit.price }}元/斤）：</label>
      <input
          type="number"
          min="0"
          v-model.number="fruit.weight"
          class="input"
      />
      <span>斤</span>
    </div>
    <div class="btn-area">
      <button class="btn" @click="checkout">结算</button>
    </div>
    <div v-if="total !== null" class="result">
      <strong>总价：<span class="total">{{ total }}</span> 元</strong>
    </div>
    <div v-if="showReset" class="reset-btn">
      <button class="btn btn-reset" @click="resetCart">重新购物</button>
    </div>
    <div v-if="error" class="error">{{ error }}</div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';

const fruits = ref([
  { name: '苹果', price: 8, weight: 0 },
  { name: '草莓', price: 13, weight: 0 },
  { name: '芒果', price: 20, weight: 0 }
]);

const option = ref(1);
const total = ref(null);
const showReset = ref(false);
const error = ref('');

const availableFruits = computed(() => {
  if (option.value === 1) return fruits.value.slice(0, 2);
  return fruits.value;
});

function checkout() {
  error.value = '';
  let sum = 0;
  let strawberryDiscount = (option.value === 3 || option.value === 4) ? 0.8 : 1;
  let fullReduction = (option.value === 4);

  const hasItem = availableFruits.value.some(fruit => fruit.weight > 0);
  if (!hasItem) {
    error.value = '请至少选择一种水果并输入购买数量！';
    return;
  }

  for (const fruit of availableFruits.value) {
    if (fruit.name === '草莓') {
      sum += fruit.price * fruit.weight * strawberryDiscount;
    } else {
      sum += fruit.price * fruit.weight;
    }
  }
  if (fullReduction && sum >= 100) {
    sum -= 10;
  }
  total.value = sum.toFixed(2);
  showReset.value = true;
}

function resetCart() {
  fruits.value.forEach(fruit => fruit.weight = 0);
  total.value = null;
  showReset.value = false;
  error.value = '';
}
</script>

<style scoped>
.container {
  max-width: 440px;
  margin: 60px auto;
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.10);
  padding: 38px 32px 28px 32px;
  font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
  transition: box-shadow 0.3s;
}
h2 {
  text-align: center;
  margin-bottom: 32px;
  font-weight: 700;
  letter-spacing: 2px;
  font-size: 2.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
}
.cart-icon {
  font-size: 2.1rem;
  margin-right: 10px;
}
.gradient-text {
  background: linear-gradient(90deg, #43e97b 0%, #38f9d7 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.form-group {
  margin-bottom: 22px;
  display: flex;
  align-items: center;
}
.fruit-row label {
  width: 120px;
}
.input, .select {
  width: 70px;
  padding: 6px 10px;
  border: 1.5px solid #d0d0d0;
  border-radius: 6px;
  margin: 0 10px;
  font-size: 16px;
  transition: border 0.2s, box-shadow 0.2s;
  outline: none;
}
.input:focus, .select:focus {
  border: 1.5px solid #43e97b;
  box-shadow: 0 0 0 2px #43e97b33;
}
.select {
  width: 320px;
  background: #f8f8f8;
}
.btn {
  background: linear-gradient(90deg, #43e97b 0%, #38f9d7 100%);
  color: #fff;
  border: none;
  padding: 10px 38px;
  border-radius: 8px;
  font-size: 18px;
  cursor: pointer;
  margin-top: 10px;
  font-weight: 600;
  box-shadow: 0 2px 8px #43e97b22;
  transition: background 0.2s, box-shadow 0.2s;
}
.btn:hover {
  background: linear-gradient(90deg, #38f9d7 0%, #43e97b 100%);
  box-shadow: 0 4px 16px #38f9d733;
}
.btn-reset {
  background: #f5f5f5;
  color: #333;
  border: 1px solid #bbb;
  margin-left: 10px;
  box-shadow: none;
}
.btn-reset:hover {
  background: #e0e0e0;
}
.result {
  margin-top: 28px;
  font-size: 22px;
  text-align: center;
}
.total {
  color: #43e97b;
  font-size: 28px;
  font-weight: bold;
  margin-left: 4px;
}
.reset-btn {
  text-align: center;
  margin-top: 14px;
}
.error {
  color: #e53935;
  margin-top: 16px;
  text-align: center;
  font-size: 16px;
  letter-spacing: 1px;
}
@media (max-width: 600px) {
  .container {
    max-width: 98vw;
    padding: 18px 4vw 18px 4vw;
  }
  .select {
    width: 90vw;
    min-width: 0;
  }
}
.fruit-row label {
  width: 140px;
  white-space: nowrap;
  margin-right: 8px;
}
.btn-area {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}
</style>